/**
 * 表格请求数据
 * @param {*} root vue的实例对象this
 * @param {*} url 请求的url地址
 * @param {*} condition 请求查询时是否有条件
 */
export async function getTableData(root, url, condition = void 0) {
    const { data: res } = await root.$http({
        url,
        method: "get",
        data: condition,
    });
    if (res.status == 200) {
        res.data.forEach((item) => {
            item.sex && (item.sex_text = item.sex == 1 ? "男" : "女");
            item.state &&
                (item.state_text =
                    item.state == 1 ? "已入学" : item.state == 2 ? "未入学" : "休学中");
            item.completed != null &&
                (item.completed_text = item.completed ? "是" : "否");
        });
    }
    return res;
}

/**
 *
 * @param {*} method 请求方法类型
 * @param {*} rowData 需要修改或增添的这一行数据
 * @returns
 */
export async function changeTableData(root, url, method, rowData) {
    const { data: res } = await root.$http({
        url,
        method,
        data: rowData,
    });

    // console.log(res);
    message(root, res.status, res.message);
    return res.data;
}

/**
 *
 * @param {*} id 根据id进行一个删除
 * @returns
 */
export async function deleteTableData(root, url, id) {
    const { data: res } = await root.$http({
        url: `${url}/${id}`,
        method: "delete",
    });
    message(root, res.status, res.message);
    return res;
}

function message(root, status, message) {
    root.$message({
        type: status == 200 ? "success" : "error",
        message,
    });
}